/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package data;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import config.Debug;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class SQLFileParser {
	private File sqlFile;
	List<String> statements = new ArrayList<String>();

	public SQLFileParser(File file) {
		this.sqlFile = file;
		this.read();
	}

	/**
	 * reads the sql script file and put statements in a list
	 */
	private void read() {
		if (sqlFile == null || !sqlFile.exists() || sqlFile.isDirectory()) {
			// nothing to read...
			return;
		}

		Scanner s = null;

		try {
			s = new Scanner(sqlFile);
		} catch (FileNotFoundException ex) {
			Debug.log("SQLFileParser.read(): Cannot find the file to be read " + sqlFile.getAbsolutePath(), Debug.ERROR_LEVEL);
			return;
		}
		s.useDelimiter(";\n");
		while (s.hasNext()) {
			statements.add(s.next());
		}
		s.close();
	}

	/**
	 * executes the statements stored in the SQLFileParser
	 */
	public void executeStatements() {
		for (String statement : statements) {
			try {
				config.Helper.localMysqlExecute(statement);
			} catch (SQLException ex) {
				Debug.log("SQLFileParser: SQLException: " + ex.getMessage(), Debug.EXCEPTION_LEVEL);
				Debug.msg("SQLFileParser: SqlException: " + ex.getMessage());
			}
		}
	}

	public static void main(String[] args) {
		File f = new File("resources/sql/player_db.sql");
		SQLFileParser p = new SQLFileParser(f);
		p.executeStatements();
		CSVToMysqlConverter c = new CSVToMysqlConverter();
		/*
		 * c.put("resources/csv/container.csv", "container");
		 * c.put("resources/csv/content.csv", "content");
		 * c.put("resources/csv/page.csv", "page");
		 * c.put("resources/csv/playlist.csv", "playlist");
		 * c.put("resources/csv/playlist_record.csv", "playlist_record");
		 */

	}
}
